package com.example.light_fragment.db;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.DatabaseUtils;
import android.database.sqlite.SQLiteDatabase;

public class DBManager {
	public static final String DATABASE_NAME = "light.db";
	public static final String TABLE_NAME = "user_basic_info";
	public static final String ID = "_id";
	public static final String USER_ID = "user_id";
	public static final String USER_NAME = "user_name";
	public static final String EMAIL = "email";
	public static final String PASSWORD = "password";
	public static final String USER_PHONE = "telephone";
	private String packageName;
	private InputStream inputStream;
	private Context context;
	private SQLiteDatabase db;

	public DBManager(String packageName, InputStream inputStream) {
		this.packageName = packageName;
		this.inputStream = inputStream;
		init();
	}

	private void init() {
		// TODO Auto-generated method stub
		if (!new File("/data/data/" + packageName + "/database.db").exists()) {
			try {
				FileOutputStream out = new FileOutputStream("data/data/"
						+ packageName + "/database.db");

				byte[] buffer = new byte[1024];
				int readBytes = 0;

				while ((readBytes = inputStream.read(buffer)) != -1)
					out.write(buffer, 0, readBytes);

				inputStream.close();
				out.close();
			} catch (IOException e) {
			}
		}

		db = SQLiteDatabase.openOrCreateDatabase("/data/data/" + packageName
				+ "/database.db", null);
	}

	public boolean insertUserinfo(int user_id, String user_name,
			String password, int telephone, String email) {

		ContentValues cv = new ContentValues();
		cv.put("user_id", user_id);
		cv.put("user_name", user_name);
		cv.put("password", password);
		cv.put("telephone", telephone);
		cv.put("email", email);
		db.insert(TABLE_NAME, null, cv);
		return true;
	}

	public Cursor getDate(int user_id) {

		Cursor res = db.rawQuery(
				"select * from from user_basic_info where user_id=" + user_id,
				null);
		return res;
	}

	public int numberofRows() {
		int numRows = (int) DatabaseUtils.queryNumEntries(db, TABLE_NAME);
		return numRows;
	}

	public boolean updateUserInfo(Integer user_id, String user_name,
			String password, int telephone, String email) {
		ContentValues cv = new ContentValues();
		cv.put("user_name", user_name);
		cv.put("password", password);
		cv.put("telephone", telephone);
		cv.put("email", email);
		db.update(TABLE_NAME, cv, "user_id=?",
				new String[] { Integer.toString(user_id) });
		return true;
	}

	public Integer deleteUserInfo(Integer user_id) {
		return db.delete(TABLE_NAME, "user_id=?",
				new String[] { Integer.toString(user_id) });
	}

	public ArrayList getAllUserInfo() {
		ArrayList array_list = new ArrayList();
		Cursor res = db.rawQuery("select * from user_basic_info", null);
		res.moveToFirst();
		while (res.isAfterLast() == false) {
			array_list.add(res.getString(res.getColumnIndex("user_name")));
			res.moveToNext();
		}
		return array_list;
	}
}
